import streamlit as st
from datetime import date, timedelta
from database.models.base import Sex
from database.queries.user_orm import SynOrmUserQueries
from database.queries.purpose_orm import SynOrmPurposeQueries
from database.queries.physical_activity_orm import SynOrmPhysicalActivityQueries


def __try_update_user(key_base):
    if SynOrmUserQueries.update_user(
        username=st.session_state["username"],
        name=st.session_state[f"{key_base}_update_name"],
        birth=st.session_state[f"{key_base}_update_birth"],
        weight=st.session_state[f"{key_base}_update_weight"],
        height=st.session_state[f"{key_base}_update_height"],
        sex=st.session_state[f"{key_base}_update_sex"],
        purpose_name=st.session_state[f"{key_base}_update_purpose_name"],
        physical_activity_name=st.session_state[f"{key_base}_update_physical_activity_name"],
    ):
        st.toast("Данные обновлены!", icon="🎊")
        

def profile_update(key_base="default"):
    user = None
    if "username" in st.session_state:
        user = SynOrmUserQueries.get_user_by_username(
            st.session_state["username"]
        )
    if not user:
        return
    
    with st.form(key=f"{key_base}_update_profile"):
        st.header("Обновление данных профиля")
        st.text_input("Имя", value=user.name, key=f"{key_base}_update_name")
        st.date_input(
            label="Дата рождения", 
            format="DD.MM.YYYY", 
            value=user.birth,
            min_value=date(1900, 1, 1), 
            max_value=(date.today()-timedelta(days=18*365.25+1)), # only 18+ users
            key=f"{key_base}_update_birth",
        )
        st.number_input(
            label="Вес (кг)", 
            value=user.weight, 
            min_value=1, 
            key=f"{key_base}_update_weight",
        )
        st.number_input(
            label="Рост (см)", 
            value=user.height, 
            min_value=1, 
            key=f"{key_base}_update_height",
        )
        sex_names = [e.name for e in Sex]
        st.selectbox(
            label="Пол", 
            options=sex_names, 
            index=sex_names.index(user.sex.name),
            key=f"{key_base}_update_sex",
        )
        purposes = SynOrmPurposeQueries.get_purposes_names()
        st.selectbox(
            label="Цель", 
            options=purposes,
            index=purposes.index(user.purpose_name),
            key=f"{key_base}_update_purpose_name",
        )
        physical_activities = \
            SynOrmPhysicalActivityQueries.get_physical_activities_names()
        st.selectbox(
            label="Физическая активность", 
            options=physical_activities,
            index=physical_activities.index(user.physical_activity_name),
            key=f"{key_base}_update_physical_activity_name",
        )
        st.form_submit_button(
            label="Обновить", 
            type="primary", 
            on_click=__try_update_user, 
            kwargs={"key_base": key_base},
        )
